<template>
  <div class="title">
    <span>一、基本情况一览表</span>
  </div>
  <el-descriptions :column="1" border>
    <el-descriptions-item label="招标人名称">{{ storeData.unitName }}</el-descriptions-item>
    <el-descriptions-item label="招标代理机构名称">中诚智信工程咨询集团股份有限公司</el-descriptions-item>
    <el-descriptions-item label="工程名称">{{ storeData.projectName }}</el-descriptions-item>
    <el-descriptions-item label="招标范围">{{ storeData.projectName }}</el-descriptions-item>
  </el-descriptions>
  <el-descriptions :column="2" border>
    <el-descriptions-item label="开标时间">{{ storeData.openBidTime }}</el-descriptions-item>
    <el-descriptions-item label="开标地点">{{ storeData.openBidPlace }}</el-descriptions-item>
    <el-descriptions-item label="评标时间">{{ storeData.openBidTime }}</el-descriptions-item>
    <el-descriptions-item label="评标地点">{{ storeData.openBidPlace }}</el-descriptions-item>
  </el-descriptions>
  <el-descriptions :column="1" border>
    <el-descriptions-item label="与评标相关的工作人员" />
    <el-descriptions-item label="投标人名单">
      <div v-for="item in bidderSList" :key="item">
        {{ item }}
      </div>
    </el-descriptions-item>
  </el-descriptions>
  <div class="title">
    <span>二、评标委员会成员名单</span>
  </div>
  <el-descriptions :column="1" border>
    <el-descriptions-item label="招标人或者招标代理机构的代表">
      <div class="table-width">
        <CommonTable :table-data="apiData.bidAgent" :columns="bidAgentColumns" />
      </div>
    </el-descriptions-item>
  </el-descriptions>
  <el-descriptions :column="1" border>
    <el-descriptions-item label="有关技术、经济等方面的评标专家">
      <div class="table-width">
        <CommonTable :table-data="apiData.otherJudges" :columns="otherJudgesColumns" />
      </div>
    </el-descriptions-item>
  </el-descriptions>
  <div class="title">
    <span>三、评标方法和评标标准</span>
  </div>
  <el-descriptions :column="1" border>
    <el-descriptions-item label="评标方法">
      <el-radio-group v-model="bidEvaluationMethod">
        <el-radio v-for="item in bidEvaluationMethodList" :key="item" :value="item" :label="item">
          {{ item }}
        </el-radio>
      </el-radio-group>
    </el-descriptions-item>
    <el-descriptions-item label="评标标准">具体明细见招标文件</el-descriptions-item>
  </el-descriptions>
  <div class="title">
    <span>四、开标记录</span>
  </div>
  <el-descriptions :column="1" border>
    <el-descriptions-item label="工程名称">{{ storeData.projectName }}</el-descriptions-item>
    <el-descriptions-item label="开标时间">{{ storeData.openBidTime }}</el-descriptions-item>
    <el-descriptions-item label="投标人名称">
      <div class="table-width">
        <el-table :data="bidderNameList" border>
          <el-table-column align="center" header-align="center" type="index" width="70" label="序号" />
          <el-table-column align="center" header-align="center" prop="supplierName" label="投标人名称" />
          <el-table-column align="center" header-align="center" label="投标文件密封情况" />
          <el-table-column align="center" header-align="center" prop="reviewPrice" label="投标报价(元)" />
          <el-table-column
            v-if="bidderList?.includes('quality')"
            align="center"
            header-align="center"
            prop="quality"
            label="质量"
          />
          <el-table-column
            v-if="bidderList?.includes('duration')"
            align="center"
            header-align="center"
            prop="duration"
            label="工期"
          />
          <el-table-column
            v-if="bidderList?.includes('projectManager')"
            align="center"
            header-align="center"
            prop="projectManager"
            label="项目负责人"
          />
          <el-table-column align="center" header-align="center" label="开标情况记录" />
          <el-table-column align="center" header-align="center" label="投标人签认" />
        </el-table>
      </div>
    </el-descriptions-item>
    <el-descriptions-item label="投标文件密封情况检查人单位、姓名:" />
    <el-descriptions-item label="记录人:" />
  </el-descriptions>
  <div class="title">
    <span>五、投标文件初步评审结论一览表</span>
  </div>
  <el-descriptions :column="1" border>
    <el-descriptions-item class-name="hidden-value" label-class-name="center-label" label="废标情况" />
  </el-descriptions>
  <el-descriptions :column="1" border>
    <el-descriptions-item label="废标原因">
      <div class="table-width">
        <CommonTable :table-data="reasonForRejectionOfBid" :columns="reasonForRejectionOfBidColumns" />
      </div>
    </el-descriptions-item>
  </el-descriptions>
  <el-descriptions :column="1" border>
    <el-descriptions-item
      class-name="hidden-value"
      label-class-name="center-label"
      label="投标文件符合招标文件要求的投标人"
    />
  </el-descriptions>
  <el-descriptions :column="1" border>
    <el-descriptions-item label="投标文件符合招标文件要求的投标人">
      <div class="table-width">
        <CommonTable :table-data="meetRequirementsBidding" :columns="meetRequirementsBiddingColumns" />
      </div>
    </el-descriptions-item>
    <el-descriptions-item label="评委（签名）：" />
  </el-descriptions>
  <div class="title">
    <span>六、经评审的价格或者评分比较一览表</span>
  </div>
  <div class="info-text">
    本项应载明评标委员会对投标文件进行详细评审和比较的结论,具体格式由评标委员会根据招标文件的具体规定自行编制。
  </div>
  <div class="title">
    <span>七、投标人排序及推荐的中标候选人名单</span>
  </div>
  <el-descriptions :column="1" border>
    <el-descriptions-item label="投标人排序">
      <div class="table-width">
        <CommonTable
          :show-index="false"
          :table-data="meetRequirementsBidding"
          :columns="meetRequirementsBiddingColumns2"
        />
      </div>
    </el-descriptions-item>
    <el-descriptions-item label="推荐的中标候选人">
      <div class="table-width">
        <CommonTable
          :show-index="false"
          :table-data="recommendedWinningBidderCandidates"
          :columns="meetRequirementsBiddingColumns3"
        />
      </div>
    </el-descriptions-item>
  </el-descriptions>
  <div class="info-text">评标委员会负责人(签名)</div>
  <div class="info-text">评标委员会成员(签名)</div>
  <div class="flex items-center justify-center mt-4">
    <el-button type="primary" @click="handleSave">提交</el-button>
  </div>
</template>
<script setup lang="ts">
import CommonTable from './CommonTable.vue'
import { getReviewReportDynamicValue, updateReviewReportDynamicValue } from '@/api/review-report-generation'

const bidAgentColumns = [
  {
    prop: 'name',
    label: '姓名'
  },
  {
    prop: 'unit',
    label: '工作单位'
  },
  {
    prop: '',
    label: '专业技术职称或者执业资格'
  },
  {
    prop: '',
    label: '在评标委员会中担任的工作'
  },
  {
    prop: '',
    label: '备注'
  }
]
const otherJudgesColumns = [
  {
    prop: 'name',
    label: '姓名'
  },
  {
    prop: 'unit',
    label: '工作单位'
  },
  {
    prop: '',
    label: '专业技术职称或者执业资格'
  },
  {
    prop: '',
    label: '在评标委员会中担任的工作'
  },
  {
    prop: '',
    label: '备注'
  }
]

const reasonForRejectionOfBidColumns = [
  {
    prop: 'supplierNames',
    label: '投标人名称'
  },
  {
    prop: 'comment',
    label: '废标原因'
  }
]
const meetRequirementsBiddingColumns = [
  {
    prop: 'supplierName',
    label: '投标人名称'
  }
]
const meetRequirementsBiddingColumns2 = [
  {
    prop: 'rankNo',
    label: '排名',
    width: 70
  },
  {
    prop: 'supplierName',
    label: '投标人名称'
  }
]
const meetRequirementsBiddingColumns3 = [
  {
    prop: 'rankNo',
    label: '排名',
    width: 70
  },
  {
    prop: 'supplierName',
    label: '投标人名称'
  },
  {
    prop: 'position',
    label: '签订合同前需处理的事宜'
  }
]

onMounted(() => {
  initData()
})
const bidderNameList = ref<any>([])
const initData = async () => {
  try {
    const res: any = await getReviewReportDynamicValue()
    bidderSList.value = res.deprecated ? [] : res.winBidResult?.map((item: any) => item.supplierName)
    bidderNameList.value = res.winBidResult
    apiData.value = res
    bidEvaluationMethod.value = res.bidMethod
    bidderList.value = res?.bidRecordShowField
    reasonForRejectionOfBid.value = getReasonForRejectionOfBid(res?.invalidRecords ?? [])
    meetRequirementsBidding.value = getMeetRequirementsBidding(
      res.deprecated,
      res?.winBidResult ?? [],
      res?.invalidRecords ?? []
    )
    recommendedWinningBidderCandidates.value = getRecommendedWinningBidderCandidates(
      res.deprecated,
      res?.winBidResult ?? [],
      res?.invalidRecords ?? []
    )
  } catch (error) {
    console.log('🐷 🐽 initData 🐽 error👉👉👉', error)
  }
}
const storeData = useProjectInfoStore().projectInfo
const apiData = ref<any>({})
// 投标人名单
const bidderSList = ref<any>([])
// 投标人列
const bidderList = ref<any>([])
// 评标方法
const bidEvaluationMethod = ref<any>([])
const bidEvaluationMethodList = ['合理低价法', '综合评估法', '经评审的最低投标价法']

// 废标原因
const reasonForRejectionOfBid = ref<any>([])
const getReasonForRejectionOfBid = (arr: any) => {
  return arr.filter((item: any) => item.stepName === '无效标')
}
const recommendedWinningBidderCandidates = ref<any>([])
const getRecommendedWinningBidderCandidates = (deprecated, winBidArr: any, invalidArr: any) => {
  if (deprecated) {
    return []
  } else {
    const temp = winBidArr.filter((item: any) => !invalidArr.some((invalid: any) => invalid.supplierIds === item.id))
    return temp.filter((item: any) => item.rankNo === 1 || item.rankNo === 2 || item.rankNo === 3)
  }
}
// 符合招标文件要求的投标人、投标人排序、推荐的中标候选人
const meetRequirementsBidding = ref<any>([])
const getMeetRequirementsBidding = (deprecated, winBidArr: any, invalidArr: any) => {
  if (deprecated) {
    return []
  } else {
    return winBidArr.filter((item: any) => !invalidArr.some((invalid: any) => invalid.supplierIds === item.id))
  }
}

const handleSave = async () => {
  await updateReviewReportDynamicValue({
    bidMethod: bidEvaluationMethod.value
  })
  await initData()
}
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
